home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevpbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  30.5 KB  |  1,000 lines

  1. /* Copyright (C) 1992, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevpbm.c,v 1.3 2000/09/23 04:52:57 lpd Exp $ */
  20. /* Portable Bit/Gray/PixMap drivers */
  21. #include "gdevprn.h"
  22. #include "gscdefs.h"
  23. #include "gscspace.h" /* For pnm_begin_typed_image(..) */
  24. #include "gxgetbit.h"
  25. #include "gxlum.h"
  26. #include "gxiparam.h" /* For pnm_begin_typed_image(..) */
  27. #include "gdevmpla.h"
  28. #include "gdevplnx.h"
  29. #include "gdevppla.h"
  30.  
  31. /*
  32.  * Thanks are due to Jos Vos (jos@bull.nl) for an earlier P*M driver,
  33.  * on which this one is based; to Nigel Roles (ngr@cotswold.demon.co.uk),
  34.  * for the plan9bm changes; and to Leon Bottou (leonb@research.att.com)
  35.  * for the color detection code in pnm_begin_typed_image.
  36.  */
  37.  
  38. /*
  39.  * There are 7 (families of) drivers here, plus one less related one:
  40.  *      pbm[raw] - outputs PBM (black and white).
  41.  *      pgm[raw] - outputs PGM (gray-scale).
  42.  *      pgnm[raw] - outputs PBM if the page contains only black and white,
  43.  *        otherwise PGM.
  44.  *      ppm[raw] - outputs PPM (RGB).
  45.  *      pnm[raw] - outputs PBM if the page contains only black and white,
  46.  *        otherwise PGM if the page contains only gray shades,
  47.  *        otherwise PPM.
  48.  *      pkm[raw] - computes internally in CMYK, outputs PPM (RGB).
  49.  *      pksm[raw] - computes internally in CMYK, outputs 4 PBM pages.
  50.  *      plan9bm - outputs Plan 9 bitmap format.
  51.  */
  52.  
  53. /*
  54.  * The code here is designed to work with variable depths for PGM and PPM.
  55.  * The code will work with any of the values in brackets, but the
  56.  * Ghostscript imager requires that depth be a power of 2 or be 24,
  57.  * so the actual allowed values are more limited.
  58.  *      pgm, pgnm: 1, 2, 4, 8, 16.  [1-16]
  59.  *      pgmraw, pgnmraw: 1, 2, 4, 8.  [1-8]
  60.  *      ppm, pnm: 4(3x1), 8(3x2), 16(3x5), 24(3x8), 32(3x10).  [3-32]
  61.  *      ppmraw, pnmraw: 4(3x1), 8(3x2), 16(3x5), 24(3x8).  [3-24]
  62.  *      pkm, pkmraw: 4(4x1), 8(4x2), 16(4x4), 32(4x8).  [4-32]
  63.  *    pksm, pksmraw: ibid.
  64.  */
  65.  
  66. /* Structure for P*M devices, which extend the generic printer device. */
  67.  
  68. #define MAX_COMMENT 70        /* max user-supplied comment */
  69. struct gx_device_pbm_s {
  70.     gx_device_common;
  71.     gx_prn_device_common;
  72.     /* Additional state for P*M devices */
  73.     char magic;            /* n for "Pn" */
  74.     char comment[MAX_COMMENT + 1];    /* comment for head of file */
  75.     byte is_raw;        /* 1 if raw format, 0 if plain */
  76.     byte optimize;        /* 1 if optimization OK, 0 if not */
  77.     byte uses_color;        /* 0 if image is black and white, */
  78.                 /* 1 if gray (PGM or PPM only), */
  79.                 /* 2 or 3 if colored (PPM only) */
  80.     bool UsePlanarBuffer;    /* 0 if chunky buffer, 1 if planar */
  81.     dev_proc_copy_alpha((*save_copy_alpha));
  82.     dev_proc_begin_typed_image((*save_begin_typed_image));
  83. };
  84. typedef struct gx_device_pbm_s gx_device_pbm;
  85.  
  86. /* ------ The device descriptors ------ */
  87.  
  88. /*
  89.  * Default X and Y resolution.
  90.  */
  91. #define X_DPI 72
  92. #define Y_DPI 72
  93.  
  94. /* Macro for generating P*M device descriptors. */
  95. #define pbm_prn_device(procs, dev_name, magic, is_raw, num_comp, depth, max_gray, max_rgb, optimize, x_dpi, y_dpi, print_page)\
  96. {    prn_device_body(gx_device_pbm, procs, dev_name,\
  97.       DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS, x_dpi, y_dpi,\
  98.       0, 0, 0, 0,\
  99.       num_comp, depth, max_gray, max_rgb, max_gray + 1, max_rgb + 1,\
  100.       print_page),\
  101.     magic,\
  102.      { 0 },\
  103.     is_raw,\
  104.     optimize,\
  105.     0, 0, 0\
  106. }
  107.  
  108. /* For all but PBM, we need our own color mapping and alpha procedures. */
  109. private dev_proc_map_rgb_color(pgm_map_rgb_color);
  110. private dev_proc_map_rgb_color(ppm_map_rgb_color);
  111. private dev_proc_map_color_rgb(pgm_map_color_rgb);
  112. private dev_proc_map_color_rgb(ppm_map_color_rgb);
  113. private dev_proc_map_cmyk_color(pkm_map_cmyk_color);
  114. private dev_proc_map_color_rgb(pkm_map_color_rgb);
  115. private dev_proc_get_params(ppm_get_params);
  116. private dev_proc_put_params(ppm_put_params);
  117. private dev_proc_copy_alpha(pnm_copy_alpha);
  118. private dev_proc_begin_typed_image(pnm_begin_typed_image);
  119.  
  120. /* We need to initialize uses_color when opening the device, */
  121. /* and after each showpage. */
  122. private dev_proc_open_device(ppm_open);
  123. private dev_proc_output_page(ppm_output_page);
  124.  
  125. /* And of course we need our own print-page routines. */
  126. private dev_proc_print_page(pbm_print_page);
  127. private dev_proc_print_page(pgm_print_page);
  128. private dev_proc_print_page(ppm_print_page);
  129. private dev_proc_print_page(pkm_print_page);
  130. private dev_proc_print_page(psm_print_page);
  131.  
  132. /* The device procedures */
  133.  
  134. /* See gdevprn.h for the template for the following. */
  135. #define pgpm_procs(p_open, p_get_params, p_map_rgb_color, p_map_color_rgb, p_map_cmyk_color) {\
  136.     p_open, NULL, NULL, ppm_output_page, gdev_prn_close,\
  137.     p_map_rgb_color, p_map_color_rgb, NULL, NULL, NULL, NULL, NULL, NULL,\
  138.     p_get_params, ppm_put_params,\
  139.     p_map_cmyk_color, NULL, NULL, NULL, gx_page_device_get_page_device\
  140. }
  141.  
  142. private const gx_device_procs pbm_procs =
  143.     pgpm_procs(gdev_prn_open, gdev_prn_get_params,
  144.            gdev_prn_map_rgb_color, gdev_prn_map_color_rgb, NULL);
  145. private const gx_device_procs pgm_procs =
  146.     pgpm_procs(ppm_open, gdev_prn_get_params,
  147.            pgm_map_rgb_color, pgm_map_color_rgb, NULL);
  148. private const gx_device_procs ppm_procs =
  149.     pgpm_procs(ppm_open, ppm_get_params,
  150.            gx_default_rgb_map_rgb_color, ppm_map_color_rgb, NULL);
  151. private const gx_device_procs pnm_procs =
  152.     pgpm_procs(ppm_open, ppm_get_params,
  153.            ppm_map_rgb_color, ppm_map_color_rgb, NULL);
  154. private const gx_device_procs pkm_procs =
  155.     pgpm_procs(ppm_open, ppm_get_params,
  156.            NULL, cmyk_1bit_map_color_rgb, cmyk_1bit_map_cmyk_color);
  157.  
  158. /* The device descriptors themselves */
  159. const gx_device_pbm gs_pbm_device =
  160. pbm_prn_device(pbm_procs, "pbm", '1', 0, 1, 1, 1, 0, 0,
  161.            X_DPI, Y_DPI, pbm_print_page);
  162. const gx_device_pbm gs_pbmraw_device =
  163. pbm_prn_device(pbm_procs, "pbmraw", '4', 1, 1, 1, 1, 1, 0,
  164.            X_DPI, Y_DPI, pbm_print_page);
  165. const gx_device_pbm gs_pgm_device =
  166. pbm_prn_device(pgm_procs, "pgm", '2', 0, 1, 8, 255, 0, 0,
  167.            X_DPI, Y_DPI, pgm_print_page);
  168. const gx_device_pbm gs_pgmraw_device =
  169. pbm_prn_device(pgm_procs, "pgmraw", '5', 1, 1, 8, 255, 0, 0,
  170.            X_DPI, Y_DPI, pgm_print_page);
  171. const gx_device_pbm gs_pgnm_device =
  172. pbm_prn_device(pgm_procs, "pgnm", '2', 0, 1, 8, 255, 0, 1,
  173.            X_DPI, Y_DPI, pgm_print_page);
  174. const gx_device_pbm gs_pgnmraw_device =
  175. pbm_prn_device(pgm_procs, "pgnmraw", '5', 1, 1, 8, 255, 0, 1,
  176.            X_DPI, Y_DPI, pgm_print_page);
  177. const gx_device_pbm gs_ppm_device =
  178. pbm_prn_device(ppm_procs, "ppm", '3', 0, 3, 24, 255, 255, 0,
  179.            X_DPI, Y_DPI, ppm_print_page);
  180. const gx_device_pbm gs_ppmraw_device =
  181. pbm_prn_device(ppm_procs, "ppmraw", '6', 1, 3, 24, 255, 255, 0,
  182.            X_DPI, Y_DPI, ppm_print_page);
  183. const gx_device_pbm gs_pnm_device =
  184. pbm_prn_device(pnm_procs, "pnm", '3', 0, 3, 24, 255, 255, 1,
  185.            X_DPI, Y_DPI, ppm_print_page);
  186. const gx_device_pbm gs_pnmraw_device =
  187. pbm_prn_device(pnm_procs, "pnmraw", '6', 1, 3, 24, 255, 255, 1,
  188.            X_DPI, Y_DPI, ppm_print_page);
  189. const gx_device_pbm gs_pkm_device =
  190. pbm_prn_device(pkm_procs, "pkm", '3', 0, 4, 4, 1, 1, 0,
  191.            X_DPI, Y_DPI, pkm_print_page);
  192. const gx_device_pbm gs_pkmraw_device =
  193. pbm_prn_device(pkm_procs, "pkmraw", '6', 1, 4, 4, 1, 1, 0,
  194.            X_DPI, Y_DPI, pkm_print_page);
  195. const gx_device_pbm gs_pksm_device =
  196. pbm_prn_device(pkm_procs, "pksm", '1', 0, 4, 4, 1, 1, 0,
  197.            X_DPI, Y_DPI, psm_print_page);
  198. const gx_device_pbm gs_pksmraw_device =
  199. pbm_prn_device(pkm_procs, "pksmraw", '4', 1, 4, 4, 1, 1, 0,
  200.            X_DPI, Y_DPI, psm_print_page);
  201.  
  202. /* Plan 9 bitmaps default to 100 dpi. */
  203. const gx_device_pbm gs_plan9bm_device =
  204. pbm_prn_device(pbm_procs, "plan9bm", '9', 1, 1, 1, 1, 1, 1,
  205.            100, 100, pbm_print_page);
  206.  
  207. /* ------ Initialization ------ */
  208.  
  209. /* Set the copy_alpha and color mapping procedures if necessary. */
  210. private void
  211. ppm_set_dev_procs(gx_device * pdev)
  212. {
  213.     gx_device_pbm * const bdev = (gx_device_pbm *)pdev;
  214.  
  215.     if (dev_proc(pdev, copy_alpha) != pnm_copy_alpha) {
  216.     bdev->save_copy_alpha = dev_proc(pdev, copy_alpha);
  217.     if (pdev->color_info.depth > 4)
  218.         set_dev_proc(pdev, copy_alpha, pnm_copy_alpha);
  219.     }
  220.     if (dev_proc(pdev, begin_typed_image) != pnm_begin_typed_image) {
  221.         bdev->save_begin_typed_image = dev_proc(pdev, begin_typed_image);
  222.     set_dev_proc(pdev, begin_typed_image, pnm_begin_typed_image);
  223.     }
  224.     if (bdev->color_info.num_components == 4) {
  225.     if (bdev->color_info.depth == 4) {
  226.         set_dev_proc(pdev, map_color_rgb, cmyk_1bit_map_color_rgb);
  227.         set_dev_proc(pdev, map_cmyk_color, cmyk_1bit_map_cmyk_color);
  228.     } else {
  229.         set_dev_proc(pdev, map_color_rgb, pkm_map_color_rgb);
  230.         set_dev_proc(pdev, map_cmyk_color, pkm_map_cmyk_color);
  231.     }
  232.     }
  233. }
  234.  
  235. /*
  236.  * Define a special open procedure that changes create_buf_device to use
  237.  * a planar device.
  238.  */
  239.  
  240. private int
  241. ppm_open(gx_device * pdev)
  242. {
  243.     gx_device_pbm * const bdev = (gx_device_pbm *)pdev;
  244.     int code = gdev_prn_open_planar(pdev, bdev->UsePlanarBuffer);
  245.  
  246.     if (code < 0)
  247.     return code;
  248.     bdev->uses_color = 0;
  249.     ppm_set_dev_procs(pdev);
  250.     return code;
  251. }
  252.  
  253. /* Print a page, and reset uses_color if this is a showpage. */
  254. private int
  255. ppm_output_page(gx_device * pdev, int num_copies, int flush)
  256. {
  257.     int code = gdev_prn_output_page(pdev, num_copies, flush);
  258.     gx_device_pbm * const bdev = (gx_device_pbm *)pdev;
  259.  
  260.     if (code < 0)
  261.     return code;
  262.     if (flush)
  263.     bdev->uses_color = 0;
  264.     return code;
  265. }
  266.  
  267. /* ------ Color mapping routines ------ */
  268.  
  269. /* Map an RGB color to a PGM gray value. */
  270. /* Keep track of whether the image is black-and-white or gray. */
  271. private gx_color_index
  272. pgm_map_rgb_color(gx_device * pdev, gx_color_value r, gx_color_value g,
  273.           gx_color_value b)
  274. {                /* We round the value rather than truncating it. */
  275.     gx_color_value gray =
  276.     ((r * (ulong) lum_red_weight) +
  277.      (g * (ulong) lum_green_weight) +
  278.      (b * (ulong) lum_blue_weight) +
  279.      (lum_all_weights / 2)) / lum_all_weights
  280.     * pdev->color_info.max_gray / gx_max_color_value;
  281.  
  282.     if (!(gray == 0 || gray == pdev->color_info.max_gray)) {
  283.     gx_device_pbm * const bdev = (gx_device_pbm *)pdev;
  284.  
  285.     bdev->uses_color = 1;
  286.     }
  287.     return gray;
  288. }
  289.  
  290. /* Map a PGM gray value back to an RGB color. */
  291. private int
  292. pgm_map_color_rgb(gx_device * dev, gx_color_index color,
  293.           gx_color_value prgb[3])
  294. {
  295.     gx_color_value gray =
  296.     color * gx_max_color_value / dev->color_info.max_gray;
  297.  
  298.     prgb[0] = gray;
  299.     prgb[1] = gray;
  300.     prgb[2] = gray;
  301.     return 0;
  302. }
  303.  
  304. /* Map an RGB color to a PPM color tuple. */
  305. /* Keep track of whether the image is black-and-white, gray, or colored. */
  306. private gx_color_index
  307. ppm_map_rgb_color(gx_device * pdev, gx_color_value r, gx_color_value g,
  308.           gx_color_value b)
  309. {
  310.     gx_device_pbm * const bdev = (gx_device_pbm *)pdev;
  311.     gx_color_index color = gx_default_rgb_map_rgb_color(pdev, r, g, b);
  312.     int bpc = pdev->color_info.depth / 3;
  313.     gx_color_index mask =
  314.     ((gx_color_index)1 << (pdev->color_info.depth - bpc)) - 1;
  315.  
  316.     if (!(((color >> bpc) ^ color) & mask)) { /* gray shade */
  317.     if (color != 0 && (~color & mask))
  318.         bdev->uses_color |= 1;
  319.     } else            /* color */
  320.     bdev->uses_color = 2;
  321.     return color;
  322. }
  323.  
  324. /* Map a PPM color tuple back to an RGB color. */
  325. private int
  326. ppm_map_color_rgb(gx_device * dev, gx_color_index color,
  327.           gx_color_value prgb[3])
  328. {
  329.     uint bitspercolor = dev->color_info.depth / 3;
  330.     uint colormask = (1 << bitspercolor) - 1;
  331.     uint max_rgb = dev->color_info.max_color;
  332.  
  333.     prgb[0] = ((color >> (bitspercolor * 2)) & colormask) *
  334.     (ulong) gx_max_color_value / max_rgb;
  335.     prgb[1] = ((color >> bitspercolor) & colormask) *
  336.     (ulong) gx_max_color_value / max_rgb;
  337.     prgb[2] = (color & colormask) *
  338.     (ulong) gx_max_color_value / max_rgb;
  339.     return 0;
  340. }
  341.  
  342. /* Map a CMYK color to a pixel value. */
  343. private gx_color_index
  344. pkm_map_cmyk_color(gx_device * pdev, gx_color_value c, gx_color_value m,
  345.            gx_color_value y, gx_color_value k)
  346. {
  347.     uint bpc = pdev->color_info.depth >> 2;
  348.     uint max_value = pdev->color_info.max_color;
  349.     uint cc = c * max_value / gx_max_color_value;
  350.     uint mc = m * max_value / gx_max_color_value;
  351.     uint yc = y * max_value / gx_max_color_value;
  352.     uint kc = k * max_value / gx_max_color_value;
  353.     gx_color_index color =
  354.     (((((cc << bpc) + mc) << bpc) + yc) << bpc) + kc;
  355.  
  356.     return (color == gx_no_color_index ? color ^ 1 : color);
  357. }
  358.  
  359. /* Map a CMYK pixel value to RGB. */
  360. private int
  361. pkm_map_color_rgb(gx_device * dev, gx_color_index color, gx_color_value rgb[3])
  362. {
  363.     int bpc = dev->color_info.depth >> 2;
  364.     gx_color_index cshift = color;
  365.     uint mask = (1 << bpc) - 1;
  366.     uint k = cshift & mask;
  367.     uint y = (cshift >>= bpc) & mask;
  368.     uint m = (cshift >>= bpc) & mask;
  369.     uint c = cshift >> bpc;
  370.     uint max_value = dev->color_info.max_color;
  371.     uint not_k = max_value - k;
  372.  
  373. #define CVALUE(c)\
  374.   ((gx_color_value)((ulong)(c) * gx_max_color_value / max_value))
  375.     /* We use our improved conversion rule.... */
  376.     rgb[0] = CVALUE((max_value - c) * not_k / max_value);
  377.     rgb[1] = CVALUE((max_value - m) * not_k / max_value);
  378.     rgb[2] = CVALUE((max_value - y) * not_k / max_value);
  379. #undef CVALUE
  380.     return 0;
  381. }
  382.  
  383. /* Augment get/put_params to add UsePlanarBuffer */
  384.  
  385. private int
  386. ppm_get_params(gx_device * pdev, gs_param_list * plist)
  387. {
  388.     gx_device_pbm * const bdev = (gx_device_pbm *)pdev;
  389.  
  390.     return gdev_prn_get_params_planar(pdev, plist, &bdev->UsePlanarBuffer);
  391. }
  392.  
  393. private int
  394. ppm_put_params(gx_device * pdev, gs_param_list * plist)
  395. {
  396.     gx_device_pbm * const bdev = (gx_device_pbm *)pdev;
  397.     gx_device_color_info save_info;
  398.     int ncomps = pdev->color_info.num_components;
  399.     int bpc = pdev->color_info.depth / ncomps;
  400.     int ecode = 0;
  401.     int code;
  402.     long v;
  403.     const char *vname;
  404.  
  405.     save_info = pdev->color_info;
  406.     if ((code = param_read_long(plist, (vname = "GrayValues"), &v)) != 1 ||
  407.     (code = param_read_long(plist, (vname = "RedValues"), &v)) != 1 ||
  408.     (code = param_read_long(plist, (vname = "GreenValues"), &v)) != 1 ||
  409.     (code = param_read_long(plist, (vname = "BlueValues"), &v)) != 1
  410.     ) {
  411.     if (code < 0)
  412.         ecode = code;
  413.     else if (v < 2 || v > (bdev->is_raw || ncomps > 1 ? 256 : 65536L))
  414.         param_signal_error(plist, vname,
  415.                    ecode = gs_error_rangecheck);
  416.     else if (v == 2)
  417.         bpc = 1;
  418.     else if (v <= 4)
  419.         bpc = 2;
  420.     else if (v <= 16)
  421.         bpc = 4;
  422.     else if (v <= 32 && ncomps == 3)
  423.         bpc = 5;
  424.     else if (v <= 256)
  425.         bpc = 8;
  426.     else
  427.         bpc = 16;
  428.     if (ecode >= 0) {
  429.         static const byte depths[4][16] =
  430.         {
  431.         {1, 2, 0, 4, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 16},
  432.         {0},
  433.         {4, 8, 0, 16, 16, 0, 0, 24},
  434.         {4, 8, 0, 16, 0, 0, 0, 32},
  435.         };
  436.  
  437.         pdev->color_info.depth = depths[ncomps - 1][bpc - 1];
  438.         pdev->color_info.max_gray = pdev->color_info.max_color =
  439.         (pdev->color_info.dither_grays =
  440.          pdev->color_info.dither_colors = (int)v) - 1;
  441.     }
  442.     }
  443.     if ((code = ecode) < 0 ||
  444.     (code = gdev_prn_put_params_planar(pdev, plist, &bdev->UsePlanarBuffer)) < 0
  445.     )
  446.     pdev->color_info = save_info;
  447.     ppm_set_dev_procs(pdev);
  448.     return code;
  449. }
  450.  
  451. /* Copy an alpha map, noting whether we may generate some non-black/white */
  452. /* colors through blending. */
  453. private int
  454. pnm_copy_alpha(gx_device * pdev, const byte * data, int data_x,
  455.        int raster, gx_bitmap_id id, int x, int y, int width, int height,
  456.            gx_color_index color, int depth)
  457. {
  458.     gx_device_pbm * const bdev = (gx_device_pbm *)pdev;
  459.  
  460.     if (pdev->color_info.depth < 24 ||
  461.     (color >> 8) == (color & 0xffff)
  462.     )
  463.     bdev->uses_color |= 1;
  464.     else
  465.     bdev->uses_color |= 2;
  466.     return (*bdev->save_copy_alpha) (pdev, data, data_x, raster, id,
  467.                      x, y, width, height, color, depth);
  468. }
  469.  
  470. /* Begin processing an image, noting whether we may generate some */
  471. /* non-black/white colors in the process. */
  472. private int 
  473. pnm_begin_typed_image(gx_device *dev,
  474.                       const gs_imager_state *pis, const gs_matrix *pmat,
  475.                       const gs_image_common_t *pim, const gs_int_rect *prect,
  476.                       const gx_drawing_color *pdcolor,
  477.               const gx_clip_path *pcpath,
  478.                       gs_memory_t *memory, gx_image_enum_common_t **pinfo)
  479. {
  480.     gx_device_pbm * const bdev = (gx_device_pbm *)dev;
  481.  
  482.     /* Conservatively guesses whether this operation causes color usage 
  483.        that might not be otherwise captured by ppm_map_color_rgb. */
  484.     if (pim && pim->type) {
  485.     switch (pim->type->index) {
  486.     case 1: case 3: case 4: {
  487.         /* Use colorspace to handle image types 1,3,4 */
  488.         const gs_pixel_image_t *pim1 = (const gs_pixel_image_t *)pim;
  489.  
  490.         if (pim1->ColorSpace) {
  491.         if (gs_color_space_get_index(pim1->ColorSpace) == gs_color_space_index_DeviceGray) {
  492.             if (pim1->BitsPerComponent > 1)
  493.             bdev->uses_color |= 1;
  494.         } else 
  495.             bdev->uses_color = 2;
  496.         }
  497.         break;
  498.     }
  499.     default:
  500.         /* Conservatively handles other image types */
  501.         bdev->uses_color = 2;
  502.     }
  503.     }
  504.     /* Forward to saved routine */
  505.     return (*bdev->save_begin_typed_image)(dev, pis, pmat, pim, prect, 
  506.                        pdcolor, pcpath, memory, pinfo);
  507. }
  508.  
  509.  
  510. /* ------ Internal routines ------ */
  511.  
  512. /* Print a page using a given row printing routine. */
  513. private int
  514. pbm_print_page_loop(gx_device_printer * pdev, char magic, FILE * pstream,
  515.          int (*row_proc) (P4(gx_device_printer *, byte *, int, FILE *)))
  516. {
  517.     gx_device_pbm * const bdev = (gx_device_pbm *)pdev;
  518.     uint raster = gdev_prn_raster(pdev);
  519.     byte *data = gs_alloc_bytes(pdev->memory, raster, "pbm_print_page_loop");
  520.     int lnum = 0;
  521.     int code = 0;
  522.  
  523.     if (data == 0)
  524.     return_error(gs_error_VMerror);
  525.     /* Hack.  This should be done in the callers.  */
  526.     if (magic == '9')
  527.     fprintf(pstream, "%11d %11d %11d %11d %11d ",
  528.         0, 0, 0, pdev->width, pdev->height);
  529.     else {
  530.     fprintf(pstream, "P%c\n", magic);
  531.     if (bdev->comment[0])
  532.         fprintf(pstream, "# %s\n", bdev->comment);
  533.     else
  534.         fprintf(pstream, "# Image generated by %s (device=%s)\n",
  535.             gs_product, pdev->dname);
  536.     fprintf(pstream, "%d %d\n", pdev->width, pdev->height);
  537.     }
  538.     switch (magic) {
  539.     case '1':        /* pbm */
  540.     case '4':        /* pbmraw */
  541.     case '9':        /* plan9bm */
  542.         break;
  543.     default:
  544.         fprintf(pstream, "%d\n", pdev->color_info.max_gray);
  545.     }
  546.     for (; lnum < pdev->height; lnum++) {
  547.     byte *row;
  548.  
  549.     code = gdev_prn_get_bits(pdev, lnum, data, &row);
  550.     if (code < 0)
  551.         break;
  552.     code = (*row_proc) (pdev, row, pdev->color_info.depth, pstream);
  553.     if (code < 0)
  554.         break;
  555.     }
  556.     gs_free_object(pdev->memory, data, "pbm_print_page_loop");
  557.     return (code < 0 ? code : 0);
  558. }
  559.  
  560. /* ------ Individual page printing routines ------ */
  561.  
  562. /* Print a monobit page. */
  563. private int
  564. pbm_print_row(gx_device_printer * pdev, byte * data, int depth,
  565.           FILE * pstream)
  566. {
  567.     gx_device_pbm * const bdev = (gx_device_pbm *)pdev;
  568.  
  569.     if (bdev->is_raw)
  570.     fwrite(data, 1, (pdev->width + 7) >> 3, pstream);
  571.     else {
  572.     byte *bp;
  573.     uint x, mask;
  574.  
  575.     for (bp = data, x = 0, mask = 0x80; x < pdev->width;) {
  576.         putc((*bp & mask ? '1' : '0'), pstream);
  577.         if (++x == pdev->width || !(x & 63))
  578.         putc('\n', pstream);
  579.         if ((mask >>= 1) == 0)
  580.         bp++, mask = 0x80;
  581.     }
  582.     }
  583.     return 0;
  584. }
  585. private int
  586. pbm_print_page(gx_device_printer * pdev, FILE * pstream)
  587. {
  588.     gx_device_pbm * const bdev = (gx_device_pbm *)pdev;
  589.  
  590.     return pbm_print_page_loop(pdev, bdev->magic, pstream, pbm_print_row);
  591. }
  592.  
  593. /* Print a gray-mapped page. */
  594. private int
  595. pgm_print_row(gx_device_printer * pdev, byte * data, int depth,
  596.           FILE * pstream)
  597. {                /* Note that bpp <= 8 for raw format, bpp <= 16 for plain. */
  598.     gx_device_pbm * const bdev = (gx_device_pbm *)pdev;
  599.     uint mask = (1 << depth) - 1;
  600.     /*
  601.      * If we're writing planes for a CMYK device, we have 0 = white,
  602.      * mask = black, which is the opposite of the pgm convention.
  603.      */
  604.     uint invert = (pdev->color_info.num_components == 4 ? mask : 0);
  605.     byte *bp;
  606.     uint x;
  607.     int shift;
  608.  
  609.     if (bdev->is_raw && depth == 8) {
  610.     if (invert) {
  611.         for (bp = data, x = 0; x < pdev->width; bp++, x++)
  612.         putc((byte)~*bp, pstream);
  613.     } else
  614.         fwrite(data, 1, pdev->width, pstream);
  615.     } else
  616.     for (bp = data, x = 0, shift = 8 - depth; x < pdev->width;) {
  617.         uint pixel;
  618.  
  619.         if (shift < 0) {    /* bpp = 16 */
  620.         pixel = ((uint) * bp << 8) + bp[1];
  621.         bp += 2;
  622.         } else {
  623.         pixel = (*bp >> shift) & mask;
  624.         if ((shift -= depth) < 0)
  625.             bp++, shift += 8;
  626.         }
  627.         ++x;
  628.         pixel ^= invert;
  629.         if (bdev->is_raw)
  630.         putc(pixel, pstream);
  631.         else
  632.         fprintf(pstream, "%d%c", pixel,
  633.             (x == pdev->width || !(x & 15) ? '\n' : ' '));
  634.     }
  635.     return 0;
  636. }
  637. private int
  638. pxm_pbm_print_row(gx_device_printer * pdev, byte * data, int depth,
  639.           FILE * pstream)
  640. {                /* Compress a PGM or PPM row to a PBM row. */
  641.     /* This doesn't have to be very fast. */
  642.     /* Note that we have to invert the data as well. */
  643.     int delta = (depth + 7) >> 3;
  644.     byte *src = data + delta - 1;    /* always big-endian */
  645.     byte *dest = data;
  646.     int x;
  647.     byte out_mask = 0x80;
  648.     byte out = 0;
  649.  
  650.     if (depth >= 8) {        /* One or more bytes per source pixel. */
  651.     for (x = 0; x < pdev->width; x++, src += delta) {
  652.         if (!(*src & 1))
  653.         out |= out_mask;
  654.         out_mask >>= 1;
  655.         if (!out_mask)
  656.         out_mask = 0x80,
  657.             *dest++ = out,
  658.             out = 0;
  659.     }
  660.     } else {            /* Multiple source pixels per byte. */
  661.     byte in_mask = 0x100 >> depth;
  662.  
  663.     for (x = 0; x < pdev->width; x++) {
  664.         if (!(*src & in_mask))
  665.         out |= out_mask;
  666.         in_mask >>= depth;
  667.         if (!in_mask)
  668.         in_mask = 0x100 >> depth,
  669.             src++;
  670.         out_mask >>= 1;
  671.         if (!out_mask)
  672.         out_mask = 0x80,
  673.             *dest++ = out,
  674.             out = 0;
  675.     }
  676.     }
  677.     if (out_mask != 0x80)
  678.     *dest = out;
  679.     return pbm_print_row(pdev, data, 1, pstream);
  680. }
  681. private int
  682. pgm_print_page(gx_device_printer * pdev, FILE * pstream)
  683. {
  684.     gx_device_pbm * const bdev = (gx_device_pbm *)pdev;
  685.  
  686.     return (bdev->uses_color == 0 && bdev->optimize ?
  687.         pbm_print_page_loop(pdev, bdev->magic - 1, pstream,
  688.                 pxm_pbm_print_row) :
  689.         pbm_print_page_loop(pdev, bdev->magic, pstream,
  690.                 pgm_print_row));
  691. }
  692.  
  693. /* Print a color-mapped page. */
  694. private int
  695. ppgm_print_row(gx_device_printer * pdev, byte * data, int depth,
  696.            FILE * pstream, bool color)
  697. {                /* If color=false, write only one value per pixel; */
  698.     /* if color=true, write 3 values per pixel. */
  699.     /* Note that depth <= 24 for raw format, depth <= 32 for plain. */
  700.     gx_device_pbm * const bdev = (gx_device_pbm *)pdev;
  701.     uint bpe = depth / 3;    /* bits per r/g/b element */
  702.     uint mask = (1 << bpe) - 1;
  703.     byte *bp;
  704.     uint x;
  705.     uint eol_mask = (color ? 7 : 15);
  706.     int shift;
  707.  
  708.     if (bdev->is_raw && depth == 24 && color)
  709.     fwrite(data, 1, pdev->width * (depth / 8), pstream);
  710.     else
  711.     for (bp = data, x = 0, shift = 8 - depth; x < pdev->width;) {
  712.         bits32 pixel = 0;
  713.         uint r, g, b;
  714.  
  715.         switch (depth >> 3) {
  716.         case 4:
  717.             pixel = (bits32) * bp << 24;
  718.             bp++;
  719.             /* falls through */
  720.         case 3:
  721.             pixel += (bits32) * bp << 16;
  722.             bp++;
  723.             /* falls through */
  724.         case 2:
  725.             pixel += (uint) * bp << 8;
  726.             bp++;
  727.             /* falls through */
  728.         case 1:
  729.             pixel += *bp;
  730.             bp++;
  731.             break;
  732.         case 0:    /* bpp == 4, bpe == 1 */
  733.             pixel = *bp >> shift;
  734.             if ((shift -= depth) < 0)
  735.             bp++, shift += 8;
  736.             break;
  737.         }
  738.         ++x;
  739.         b = pixel & mask;
  740.         pixel >>= bpe;
  741.         g = pixel & mask;
  742.         pixel >>= bpe;
  743.         r = pixel & mask;
  744.         if (bdev->is_raw) {
  745.         if (color) {
  746.             putc(r, pstream);
  747.             putc(g, pstream);
  748.         }
  749.         putc(b, pstream);
  750.         } else {
  751.         if (color)
  752.             fprintf(pstream, "%d %d ", r, g);
  753.         fprintf(pstream, "%d%c", b,
  754.             (x == pdev->width || !(x & eol_mask) ?
  755.              '\n' : ' '));
  756.         }
  757.     }
  758.     return 0;
  759. }
  760. private int
  761. ppm_print_row(gx_device_printer * pdev, byte * data, int depth,
  762.           FILE * pstream)
  763. {
  764.     return ppgm_print_row(pdev, data, depth, pstream, true);
  765. }
  766. private int
  767. ppm_pgm_print_row(gx_device_printer * pdev, byte * data, int depth,
  768.           FILE * pstream)
  769. {
  770.     return ppgm_print_row(pdev, data, depth, pstream, false);
  771. }
  772. private int
  773. ppm_print_page(gx_device_printer * pdev, FILE * pstream)
  774. {
  775.     gx_device_pbm * const bdev = (gx_device_pbm *)pdev;
  776.  
  777.     return (bdev->uses_color >= 2 || !bdev->optimize ?
  778.         pbm_print_page_loop(pdev, bdev->magic, pstream,
  779.                 ppm_print_row) :
  780.         bdev->uses_color == 1 ?
  781.         pbm_print_page_loop(pdev, bdev->magic - 1, pstream,
  782.                 ppm_pgm_print_row) :
  783.         pbm_print_page_loop(pdev, bdev->magic - 2, pstream,
  784.                 pxm_pbm_print_row));
  785. }
  786.  
  787. /* Print a faux CMYK page. */
  788. /* Print a row where each pixel occupies 4 bits (depth == 4). */
  789. /* In this case, we also know pdev->color_info.max_color == 1. */
  790. private int
  791. pkm_print_row_4(gx_device_printer * pdev, byte * data, int depth,
  792.         FILE * pstream)
  793. {
  794.     gx_device_pbm * const bdev = (gx_device_pbm *)pdev;
  795.     byte *bp;
  796.     uint x;
  797.     byte rv[16], gv[16], bv[16], i;
  798.  
  799.     /* Precompute all the possible pixel values. */
  800.     for (i = 0; i < 16; ++i) {
  801.     gx_color_value rgb[3];
  802.  
  803.     cmyk_1bit_map_color_rgb((gx_device *)pdev, (gx_color_index)i, rgb);
  804.     rv[i] = rgb[0] / gx_max_color_value;
  805.     gv[i] = rgb[1] / gx_max_color_value;
  806.     bv[i] = rgb[2] / gx_max_color_value;
  807.     }
  808.     /*
  809.      * Contrary to what the documentation implies, gcc compiles putc
  810.      * as a procedure call.  This is ridiculous, but since we can't
  811.      * change it, we buffer groups of pixels ourselves and use fwrite.
  812.      */
  813.     if (bdev->is_raw) {
  814.     for (bp = data, x = 0; x < pdev->width;) {
  815.         byte raw[50 * 3];    /* 50 is arbitrary, but must be even */
  816.         int end = min(x + sizeof(raw) / 3, pdev->width);
  817.         byte *outp = raw;
  818.  
  819.         for (; x < end; bp++, outp += 6, x += 2) {
  820.         uint b = *bp;
  821.         uint pixel = b >> 4;
  822.  
  823.         outp[0] = rv[pixel], outp[1] = gv[pixel], outp[2] = bv[pixel];
  824.         pixel = b & 0xf;
  825.         outp[3] = rv[pixel], outp[4] = gv[pixel], outp[5] = bv[pixel];
  826.         }
  827.         /* x might overshoot the width by 1 pixel. */
  828.         if (x > end)
  829.         outp -= 3;
  830.         fwrite(raw, 1, outp - raw, pstream);
  831.     }
  832.     } else {
  833.     int shift;
  834.  
  835.     for (bp = data, x = 0, shift = 4; x < pdev->width;) {
  836.         int pixel = (*bp >> shift) & 0xf;
  837.  
  838.         shift ^= 4;
  839.         bp += shift >> 2;
  840.         ++x;
  841.         fprintf(pstream, "%d %d %d%c", rv[pixel], gv[pixel], bv[pixel],
  842.             (x == pdev->width || !(x & 7) ?
  843.              '\n' : ' '));
  844.     }
  845.     }
  846.     return 0;
  847. }
  848. /* Print a row where each pixel occupies 1 or more bytes (depth >= 8). */
  849. private int
  850. pkm_print_row(gx_device_printer * pdev, byte * data, int depth,
  851.           FILE * pstream)
  852. {
  853.     gx_device_pbm * const bdev = (gx_device_pbm *)pdev;
  854.     byte *bp;
  855.     uint x;
  856.     ulong max_value = pdev->color_info.max_color;
  857.  
  858.     for (bp = data, x = 0; x < pdev->width;) {
  859.     bits32 pixel = 0;
  860.     gx_color_value rgb[3];
  861.     uint r, g, b;
  862.  
  863.     switch (depth >> 3) {
  864.         case 4:
  865.         pixel = (bits32) * bp << 24;
  866.         bp++;
  867.         /* falls through */
  868.         case 3:
  869.         pixel += (bits32) * bp << 16;
  870.         bp++;
  871.         /* falls through */
  872.         case 2:
  873.         pixel += (uint) * bp << 8;
  874.         bp++;
  875.         /* falls through */
  876.         case 1:
  877.         pixel += *bp;
  878.         bp++;
  879.     }
  880.     ++x;
  881.     pkm_map_color_rgb((gx_device *) pdev, pixel, rgb);
  882.     r = rgb[0] * max_value / gx_max_color_value;
  883.     g = rgb[1] * max_value / gx_max_color_value;
  884.     b = rgb[2] * max_value / gx_max_color_value;
  885.     if (bdev->is_raw) {
  886.         putc(r, pstream);
  887.         putc(g, pstream);
  888.         putc(b, pstream);
  889.     } else {
  890.         fprintf(pstream, "%d %d %d%c", r, g, b,
  891.             (x == pdev->width || !(x & 7) ?
  892.              '\n' : ' '));
  893.     }
  894.     }
  895.     return 0;
  896. }
  897. private int
  898. pkm_print_page(gx_device_printer * pdev, FILE * pstream)
  899. {
  900.     gx_device_pbm * const bdev = (gx_device_pbm *)pdev;
  901.  
  902.     return pbm_print_page_loop(pdev, bdev->magic, pstream,
  903.                    (pdev->color_info.depth < 8 ?
  904.                 pkm_print_row_4 :
  905.                 pkm_print_row));
  906. }
  907.  
  908. /* Print individual separations on a single file. */
  909. private int
  910. psm_print_page(gx_device_printer * pdev, FILE * pstream)
  911. {
  912.     gx_device_pbm * const bdev = (gx_device_pbm *)pdev;
  913.     /*
  914.      * Allocate a large enough buffer for full pixels, on the theory that we
  915.      * don't know how many bits will be allocated to each component.  (This
  916.      * is for didactic purposes only: we know perfectly well that each
  917.      * component will have 1/N of the bits.)
  918.      */
  919.     uint max_raster = bitmap_raster(pdev->width * pdev->color_info.depth);
  920.     byte *data = gs_alloc_bytes(pdev->memory, max_raster, "pksm_print_page");
  921.     int code = 0;
  922.     int plane;
  923.  
  924.     if (data == 0)
  925.     return_error(gs_error_VMerror);
  926.     for (plane = 0; plane < pdev->color_info.num_components; ++plane) {
  927.     int lnum, band_end;
  928.     /*
  929.      * The following initialization is unnecessary: lnum == band_end on
  930.      * the first pass through the loop below, so marked will always be
  931.      * set before it is used.  We initialize marked solely to suppress
  932.      * bogus warning messages from certain compilers.
  933.      */
  934.     gx_color_index marked = 0;
  935.     gx_render_plane_t render_plane;
  936.     int plane_depth;
  937.     int plane_shift;
  938.     gx_color_index plane_mask;
  939.     int raster;
  940.  
  941.     gx_render_plane_init(&render_plane, (gx_device *)pdev, plane);
  942.     plane_depth = render_plane.depth;
  943.     plane_shift = render_plane.shift;
  944.     plane_mask = (1 << plane_depth) - 1;
  945.     raster = bitmap_raster(pdev->width * plane_depth);
  946.     fprintf(pstream, "P%c\n", bdev->magic + (plane_depth > 1));
  947.     if (bdev->comment[0])
  948.         fprintf(pstream, "# %s\n", bdev->comment);
  949.     else
  950.         fprintf(pstream, "# Image generated by %s (device=%s)\n",
  951.             gs_product, pdev->dname);
  952.     fprintf(pstream, "%d %d\n", pdev->width, pdev->height);
  953.     if (plane_depth > 1)
  954.         fprintf(pstream, "%d\n", pdev->color_info.max_gray);
  955.     for (lnum = band_end = 0; lnum < pdev->height; lnum++) {
  956.         byte *row;
  957.  
  958.         if (lnum == band_end) {
  959.         gx_colors_used_t colors_used;
  960.         int band_start;
  961.         int band_height =
  962.             gdev_prn_colors_used((gx_device *)pdev, lnum, 1,
  963.                      &colors_used, &band_start);
  964.  
  965.         band_end = band_start + band_height;
  966.         marked = colors_used.or & (plane_mask << plane_shift);
  967.         if (!marked)
  968.             memset(data, 0, raster);
  969. #ifdef DEBUG
  970.         if (plane == 0)
  971.             if_debug4(':',
  972.                   "[:]%4d - %4d mask = 0x%lx, slow_rop = %d\n",
  973.                   lnum, band_end - 1, (ulong)colors_used.or,
  974.                   colors_used.slow_rop);
  975. #endif
  976.         }
  977.         if (marked) {
  978.         gx_render_plane_t render_plane;
  979.         uint actual_raster;
  980.  
  981.         render_plane.index = plane;
  982.         code = gdev_prn_get_lines(pdev, lnum, 1, data, raster,
  983.                       &row, &actual_raster,
  984.                       &render_plane);
  985.         if (code < 0)
  986.             break;
  987.         } else
  988.         row = data;
  989.         code =
  990.         (plane_depth == 1 ?
  991.          pbm_print_row(pdev, row, plane_depth, pstream) :
  992.          pgm_print_row(pdev, row, plane_depth, pstream));
  993.         if (code < 0)
  994.         break;
  995.     }
  996.     }
  997.     gs_free_object(pdev->memory, data, "pksm_print_page");
  998.     return (code < 0 ? code : 0);
  999. }
  1000.